import numpy as np
import cv2 as cv

cap = cv.VideoCapture(0)      #电脑摄像头对象
#cap = cv.VideoCapture('vtest.avi')   #可以直接改为文件名，打开视频文件
if not cap.isOpened():
    print("Cannot open camera")
    exit()

cap.set(cv.CAP_PROP_FRAME_WIDTH,320)  #设置宽度
cap.set(cv.CAP_PROP_FRAME_HEIGHT, 240) #设置高度
while True:
    ret ,frame = cap.read()  #ret返回True or False，Frame是帧
    #print(ret)
    if not ret:
        print("Can't receive frame (stream end?). Exiting ...")
        break
    print(cap.get(cv.CAP_PROP_FRAME_WIDTH))  #输出宽度
    print(cap.get(cv.CAP_PROP_FRAME_HEIGHT))  #输出高度
    gray = cv.cvtColor(frame,cv.COLOR_BGR2GRAY)
    cv.imshow('frame',gray)   #展示窗口，名字为frame
    if cv.waitKey(1) == ord('q'):
        break
#释放
cap.release()
cv.destroyAllWindows()